<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/2/20
 * Time: 11:31
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Ke extends Base
{
    /**
     * 乐器列表
     * @return mixed|\think\response\Json
     */
    public function axes(){
        if(request()->isAjax ()){
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_axe')->count();// 获取总条数
            $lists = Db::table('up_axe')
                ->page($Nowpage,$limits)
                ->order('axe_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    /**
     * 添加乐器
     * @return mixed|\think\response\Json
     */
    public function axeAdd(){
        if(Request::instance()->isPost()){
            $axe_name = trim(input('axe_name'), ' ');
            $axe_thumb = input('axe_thumb');
            $data = [
                'axe_name' => $axe_name,
                'axe_thumb' => $axe_thumb,
                'axe_admin' => session('username'),
                'axe_time' => date('Y-m-d H:i:s'),
            ];
            $res = Db::table('up_axe')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('ke/axeadd');
    }
    /**
     * 编辑乐器
     * @return mixed|\think\response\Json
     */
    public function axeEdit(){
        if(Request::instance()->isPost()){
            $axe_id = input('axe_id');
            $axe_name = trim(input('axe_name'), ' ');
            $axe_thumb = input('axe_thumb');
            $data = [
                'axe_name' => $axe_name,
                'axe_thumb' => $axe_thumb,
                'axe_admin' => session('username'),
                'axe_time' => date('Y-m-d H:i:s'),
            ];
            $res = Db::table('up_axe')->where('axe_id', $axe_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $axe_id = input('axe_id');
        // banner
        $info = Db::table('up_axe')
            ->where('axe_id', $axe_id)
            ->find();
        $this->assign([
            'axe_id' => $axe_id,
            'info' => $info
        ]);
        return $this->fetch('ke/axeedit');
    }
    /**
     * 删除
     * @return \think\response\Json
     */
    public function axedel(){
        $axe_id = input('id');
        $res = Db::table('up_axe')->where('axe_id', $axe_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 200, 'msg' => '失败']);
        }
    }
    //--------------------------------------------------------------------------
    /**
     * 课程展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax ()){
            $where = [];
            if(session('groupid') == 2){
                $where['c_piano'] = ['like','%' . session('piano') . '%'];
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_course')->alias('c')
                ->join('up_u_user u','c.c_teacher=u.user_id','LEFT')
                ->join('up_axe a','c.c_axe=a.axe_id','LEFT')
                ->where($where)
                ->count();// 获取总条数
            $field = 'c.*,user_username,user_phone,axe_name';
            $lists = Db::table('up_course')->alias('c')
                ->join('up_u_user u','c.c_teacher=u.user_id','LEFT')
                ->join('up_axe a','c.c_axe=a.axe_id','LEFT')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('c_status asc,c_time desc')
                ->field($field)
                ->select();
            if($lists){
                foreach ($lists as $key => $vo){
                    $lists[$key]['panios'] = '';
                    $panios = Db::table('up_piano')->where('pa_id','in',trim($vo['c_piano'],','))->column('pa_name');
                    if($panios){
                        $lists[$key]['panios'] = implode($panios,',');
                    }
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    /**
     * 添加
     * @return mixed
     */
    public function addCourse(){
        if(Request::instance()->isPost()){
            $c_teacher = input('c_teacher');
            $c_name = input('c_name');
            $c_thumb = input('c_thumb');
            $c_axe = input('c_axe');
            $c_piano = input('c_piano');
            $c_content = input('c_content');
            $c_num = (int)input('c_num');
            $c_status = input('c_status');
            if($c_num < 0){
                return json(['code' => 100, 'msg' => '人数不能低于0']);
            }
            $insert = [
                'c_teacher' => $c_teacher,
                'c_name' => $c_name,
                'c_thumb' => $c_thumb,
                'c_axe' => $c_axe,
                'c_piano' => $c_piano,
                'c_content' => $c_content,
                'c_num' => $c_num,
                'c_status' => $c_status,
                'c_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_course')->insertGetId($insert);
            if($res){
                $ids = Db::table('up_u_user')
                    ->where('user_status',1)
                    ->where('user_type',1)
                    ->where('user_piano','in',$c_piano)
                    ->column('user_id');
                if($ids){
                    foreach ($ids as $k => $v) {
                        try{
                            $con = '您有新的课程介绍：' . $c_name;
                            $nid = allInNotice('您有新的课程介绍',$con,7,$v,$res);
                            $this->Jpush(1, $v, '您有新的课程介绍', ['type'=>7,'nid'=>$nid]);
                        }catch(\Exception $e){

                        }
                    }
                }
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $panio = Db::table('up_piano')
            ->order('pa_id desc')
            ->select();
        $where = [
            'user_status' => 1,
            'user_type' => 2
        ];
        if(session('groupid') == 2){
            $panio = Db::table('up_piano')
                ->where('pa_id',session('piano'))
                ->select();
            $where['user_piano'] = ['like','%' . session('piano') . '%'];
        }
        $axe = Db::table('up_axe')
            ->select();
        $teachers = Db::table('up_u_user')
            ->where($where)
            ->field('user_id,user_username,user_phone')
            ->select();
        $this->assign('panio', $panio);
        $this->assign('axe', $axe);
        $this->assign('teachers', $teachers);
        return $this->fetch('ke/addcourse');
    }
    /**
     * 编辑
     * @return mixed
     */
    public function courseEdit(){
        $c_id = input('c_id');
        $info = Db::table('up_course')
            ->where('c_id', $c_id)
            ->find();
        if(Request::instance()->isPost()){
            $c_teacher = input('c_teacher');
            $c_name = input('c_name');
            $c_thumb = input('c_thumb');
            $c_axe = input('c_axe');
            $c_piano = input('c_piano');
            $c_content = input('c_content');
            $c_num = (int)input('c_num');
            $c_status = input('c_status');
            if($c_num < 0){
                return json(['code' => 100, 'msg' => '人数不能低于0']);
            }
            $insert = [
                'c_teacher' => $c_teacher,
                'c_name' => $c_name,
                'c_thumb' => $c_thumb,
                'c_axe' => $c_axe,
                'c_piano' => $c_piano,
                'c_content' => $c_content,
                'c_num' => $c_num,
                'c_status' => $c_status,
                'c_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_course')->where('c_id',$c_id)->update($insert);
            if($res){
                $ids = Db::table('up_u_user')
                    ->where('user_status',1)
                    ->where('user_type',1)
                    ->where('user_piano','in',$c_piano)
                    ->column('user_id');
                if($ids){
                    foreach ($ids as $k => $v) {
                        try{
                            $con = '您的课程介绍更新了：' . $c_name;
                            $nid = allInNotice('您的课程介绍更新了',$con,7,$v,$c_id);
                            $this->Jpush(1, $v, '您的课程介绍更新了', ['type'=>7,'nid'=>$nid]);
                        }catch(\Exception $e){

                        }
                    }
                }
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $info['c_piano'] = explode(',',$info['c_piano']);
        $panio = Db::table('up_piano')
            ->order('pa_id desc')
            ->select();
        $axe = Db::table('up_axe')
            ->select();
        $where = [
            'user_status' => 1,
            'user_type' => 2
        ];
        if(session('groupid') == 2){
            $where['user_piano'] = ['like','%' . session('piano') . '%'];
        }
        $teachers = Db::table('up_u_user')
            ->where($where)
            ->field('user_id,user_username,user_phone')
            ->select();
        $this->assign('panio', $panio);
        $this->assign('axe', $axe);
        $this->assign('teachers', $teachers);
        $this->assign('info', $info);
        $this->assign('c_id', $c_id);
        $type = 0;
        if(isset($_REQUEST['type']) && $_REQUEST['type'] == 1){
            $type = 1;
        }
        $this->assign('type', $type);
        return $this->fetch('ke/editcourse');
    }
    /**
     * 上架
     * @return \think\response\Json
     */
    public function courseup(){
        $banner_id = input('id');
        $res = Db::table('up_course')->where('c_id', $banner_id)->setField('c_status', 1);
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 200, 'msg' => '失败']);
        }
    }
    /**
     * 下架
     * @return \think\response\Json
     */
    public function coursedown(){
        $banner_id = input('id');
        $res = Db::table('up_course')->where('c_id', $banner_id)->setField('c_status', 2);
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 200, 'msg' => '失败']);
        }
    }
    /**
     * 删除
     * @return \think\response\Json
     */
    public function coursedel(){
        $banner_id = input('id');
        $res = Db::table('up_course')->where('c_id', $banner_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 200, 'msg' => '失败']);
        }
    }
    //--------------------------------------------------------------------------
    /**
     * 课程展示
     * @return mixed|\think\response\Json
     */
    public function test(){
        if(request()->isAjax ()){
            $where = [];
            if(session('groupid') == 2){
                $where['ct_p_id'] = session('piano');
            }
            $keyword = input('keyword');
            $user_school = input('user_school');
            $utype = input('ct_stats');
            if(isset($keyword) && $keyword != ''){
                $where['ct_name'] = ['like', '%' . $keyword . '%'];
            }
            //琴行
            if(isset($user_school) && $user_school != "")
            {
                $where['ct_p_id'] = $user_school;
            }
            if(isset($utype) && $utype != "")
            {
                $where['ct_status'] = $utype;
            }
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_course_test')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_course_test')
                ->where($where)
                ->order('ct_status asc,ct_time desc')
                ->page($Nowpage,$limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $panio = Db::table('up_piano')
            ->order('pa_id desc')
            ->select();
        if(session('groupid') == 2){
            $panio = Db::table('up_piano')
                ->where('pa_id',session('piano'))
                ->select();
        }
        $this->assign('panio', $panio);
        return $this->fetch('ke/test');
    }
    /**
     * 编辑
     * @return mixed
     */
    public function testEdit(){
        $ct_id = input('ct_id');
        $info = Db::table('up_course_test')
            ->where('ct_id', $ct_id)
            ->find();
        if(Request::instance()->isPost()){
            $ct_stuff = input('ct_stuff');
            $ct_status = input('ct_status');
            $ct_remark = input('ct_remark');
            $insert = [
                'ct_stuff' => $ct_stuff,
                'ct_status' => $ct_status,
                'ct_remark' => $ct_remark,
                'ct_update_time' => date('Y-m-d H:i:s')
            ];
            $res = Db::table('up_course_test')->where('ct_id',$ct_id)->update($insert);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $this->assign('info', $info);
        $this->assign('ct_id', $ct_id);
        return $this->fetch('ke/testedit');
    }
    /**
     * 删除
     * @return \think\response\Json
     */
    public function testdel(){
        $banner_id = input('id');
        $res = Db::table('up_course_test')->where('ct_id',$banner_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 200, 'msg' => '失败']);
        }
    }
}